import { h, ref, reactive } from 'vue'
import { findDictBrand,findDictTypeList} from '@/api/common'
import { dictType} from '@/utils/dict'
import { dataToSelect} from '@/utils'
import { Edit,} from '@element-plus/icons-vue'
let router:any = null

/**
 *  获取路由对象
 */
export function getRouterInstance(r) {
  router = r
}

let DT:any = null

// 认证状态
const droneAuthStatus = ref<dictType[]>([]) 
// 品牌
const droneBrand = ref<dictType[]>([]) 
// 无人机型号
const droneModelCode = ref<dictType[]>([]) 
// 无人机状态
const droneStatus = ref<dictType[]>([]) 

// 获取data对象
export function getDataInstance(data) {
  DT = data
}

interface optionsType{
  label:string
  value:string | number | null | undefined
}
interface formItemType {
  type: string,
  key: string,
  label: string,
  row: object,
  props: object
  options?: optionsType[]
}

interface OnionsType {
  modalForm:any
  formItem: formItemType[]
  modalRules?:object
}

// 基础信息
export const baseOpions = reactive({
  modalForm: {
    code:null, // 无人机实名状态(1已实名 0未实名)
    authenticationStatus:0, // 无人机实名状态(1已实名 0未实名)
    brand:null, // 无人机品牌
    modelCode:null, // 无人机型号
    status:null, // 可用状态
    name:null, // 无人机名称
    coverage:null, // 覆盖范围
  },
  formItem: [
    {
      type: 'input',
      key: 'code',
      label: '无人机编码',
      row: { sm: 24, md: 24 },
      props: { maxlength: '50', clearable: true, placeholder: '请输入发货地址' }
    },
    {
      type: 'select',
      key: 'authenticationStatus',
      label: '认证状态',
      options: droneAuthStatus,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'select',
      key: 'brand',
      label: '品牌',
      options: droneBrand,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'select',
      key: 'modelCode',
      label: '无人机型号',
      options: droneModelCode,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'select',
      key: 'status',
      label: '可用状态',
      options: droneStatus,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'input',
      key: 'name',
      label: '无人机名称',
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'input',
      key: 'coverage',
      label: '覆盖范围',
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    }
  ],
  modalRules: {
    code: [{ required: true, message: '请输入无人机编号', trigger: ['change'] }],
    authenticationStatus: [{ required: true, message: '请选择认证状态', trigger: ['change'] }],
    brand: [{ required: true, message: '请选择无人机品牌', trigger: ['change'] }],
    modelCode: [{ required: true, message: '请选择无人机型号', trigger: ['change'] }],
    status: [{ required: true, message: '请选择可用状态', trigger: ['change'] }],
    name: [{ required: true, message: '请输入无人机名称', trigger: ['change'] }],
    coverage: [{ required: true, message: '请输入覆盖范围', trigger: ['change'] }],
  },
})


// 挂载类型
const mountType = ref<dictType[]>([]) 
// 品牌
const mountBrand = ref<dictType[]>([]) 

export const mountGroup = [
  {
    type: "select",
    key: "type",
    label: "挂载设备类型：",
    options:'mountTypeArr',
    row: { sm: 6, md: 6 },
    props: { clearable: true, placeholder: "请选择" },
  },
  {
    type: "select",
    key: "brand",
    label: "品牌：",
    options: 'mountBrandArr',
    row: { sm: 6, md: 6 },
    props: { clearable: true, placeholder: "请选择" },
  },
  {
    type: "select",
    key: "series",
    label: "挂载型号：",
    options:'seriesTypeArr',
    row: { sm: 6, md: 6 },
    props: { clearable: true, placeholder: "请选择" },
  },
  {
    type: "select",
    key: "code",
    label: "挂载编码：",
    options: 'codeArr',
    row: { sm: 6, md: 6 },
    props: { clearable: true, placeholder: "请选择" },
  },
];


export const mountGroupValue = {
  code: null, // 挂载编号
  type:null, // 挂载类型
  brand:null, // 挂载品牌 drone_DJ 大疆  drone_TH 拓恒
  series:null, // 挂载系列 
  mountTypeArr:mountType,
  mountBrandArr:mountBrand,
  seriesTypeArr:[],
  codeArr:[],
}

export const mountForm = reactive({
  modalForm: [],
  mounts: [mountGroup],
});


// 操作记录
export const setpOpions = reactive([
  {
    title:'新增设备',
    description:"七尾 2023.10.10 10:10:00",
    icon:'iconfont icon-chuangjian'
  },
  {
    title:'设备关联：xx无人机',
    description:"七尾领导 2023.10.10 10:10:00",
    icon:'iconfont icon-feiji_qifei'
  },
  {
    title:'解绑设备：xx无人机',
    description:"八尾 2023.10.10 10:10:00",
    icon:'iconfont icon-riqifanwei'
  },
  {
    title:'更改设备状态：报废',
    description:"八尾 2023.10.10 10:10:00",
    icon:'iconfont icon-biaoqian1'
  }
])

//  挂载无人机类型
export const getType = async function () {
  const {data=[]} = await  findDictTypeList({prefix:'drone'}) as any
  const drone_auth_status = data.find(item=> item.dictType==='drone_auth_status').dateList?.map(item=>{
    return {
      ...item,
      dictValue:parseInt(item.dictValue)
    }
  })  ||[]
  const drone_brand = data.find(item=> item.dictType==='drone_brand').dateList ||[]
  const drone_model_code = data.find(item=> item.dictType==='drone_model_code').dateList ||[]
  const drone_status = data.find(item=> item.dictType==='drone_status').dateList?.map(item=>{
    return {
      ...item,
      dictValue:parseInt(item.dictValue)
    }
  })  ||[]
  // 认证状态
  droneAuthStatus.value = dataToSelect(drone_auth_status, { label: 'dictLabel', value: 'dictValue' })
  // 品牌
  droneBrand.value  = dataToSelect(drone_brand, { label: 'dictLabel', value: 'dictValue' }) 
  // 无人机型号
  droneModelCode.value = dataToSelect(drone_model_code, { label: 'dictLabel', value: 'dictValue' }) 
  // 可用状态
  droneStatus.value  = dataToSelect(drone_status, { label: 'dictLabel', value: 'dictValue' }) 
}

//  挂载设备类型
export const getMountType = async function () {
  const {data=[]} = await  findDictTypeList({prefix:'mount'}) as any
  const mount_type = data.find(item=> item.dictType==='mount_type').dateList ||[]
  const mount_brand = data.find(item=> item.dictType==='mount_brand').dateList ||[]
    mountType.value = dataToSelect(mount_type, { label: 'dictLabel', value: 'dictValue' }) 
    mountBrand.value  = dataToSelect(mount_brand, { label: 'dictLabel', value: 'dictValue' }) 
}

//  挂载型号
export const getSeriesType = async function (prefix) {
  const {code,data=[],msg} = await  findDictBrand({prefix}) as any
      return dataToSelect(data, { label: 'dictLabel', value: 'dictLabel' })
}


